/**
 * output package name
 */
package com.kingdee.eas.custom.hr.attendance.client;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.Date;

import org.apache.log4j.Logger;

import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.table.KDTMergeManager;
import com.kingdee.bos.ctrl.kdf.table.KDTSelectManager;
import com.kingdee.bos.ctrl.kdf.table.KDTSortManager;
import com.kingdee.bos.ctrl.swing.KDComboBox;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.bos.util.backport.Collections;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.common.Constants;
import com.kingdee.eas.custom.common.KDTableUtilEx;
import com.kingdee.eas.custom.common.PersonUtil;
import com.kingdee.eas.custom.common.TableCellComparator;
import com.kingdee.eas.custom.hr.attendance.ApproveStateEnum;
import com.kingdee.eas.custom.hr.attendance.HolidayLogInfo;
import com.kingdee.eas.custom.hr.attendance.HolidayReviewFactory;
import com.kingdee.eas.custom.hr.attendance.HolidayReviewInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.hr.rec.util.DateUtil;
import com.kingdee.eas.hr.time.TimeAttendanceInfo;
import com.kingdee.eas.hr.time.UnitTypeEnum;
import com.kingdee.eas.hr.time.client.TimeAttendanceUtils;
import com.kingdee.jdbc.rowset.IRowSet;

/**
 * output class name
 */
public class HolidayReviewUI extends AbstractHolidayReviewUI {
	private static final Logger logger = CoreUIObject
			.getLogger(HolidayReviewUI.class);

	KDTSortManager sm = new KDTSortManager(this.tblMain) {

		public void sort(final int colIndex, final int sortType) {
			super.sort(colIndex, sortType);
			Collections.sort(this.table.getBody().getRows(),
					new TableCellComparator(colIndex, sortType));
		}
	};

	/**
	 * output class constructor
	 */
	public HolidayReviewUI() throws Exception {
		super();
		this.tblMain.checkParsed();
		sm.setClickCount(2);

		this.tblMain.getColumn("person").getStyleAttributes().setHided(true);
		this.tblMain.getColumn("logId").getStyleAttributes().setHided(true);

		this.tblMain.checkParsed();

		KDComboBox tblMain_approveState_Combox = new KDComboBox();
		tblMain_approveState_Combox.setName("tblMain_approveState_Combox");
		tblMain_approveState_Combox.setVisible(true);
		tblMain_approveState_Combox.addItems(ApproveStateEnum.getEnumList()
				.toArray());
		KDTDefaultCellEditor tblMain_approveState_CellEditor = new KDTDefaultCellEditor(
				tblMain_approveState_Combox);
		this.tblMain.getColumn("approveState").setEditor(
				tblMain_approveState_CellEditor);

		this.tblMain.getColumn("date").getStyleAttributes().setNumberFormat(
				"yyyy-MM-dd");
	}

	/**
	 * output storeFields method
	 */
	public void storeFields() {
		super.storeFields();
	}

	/**
	 * output btnQuery_actionPerformed method
	 */
	protected void btnQuery_actionPerformed(java.awt.event.ActionEvent e)
			throws Exception {

		this.tblMain.removeRows();

		int approveState = ((ApproveStateEnum) this.cboApproveState
				.getSelectedItem()).getValue();
		Date beginTime = (Date) this.pkBeginDate.getValue();
		Date endTime = (Date) this.pkEndDate.getValue();
		AdminOrgUnitInfo adminOrg = (AdminOrgUnitInfo) this.prmtAdminOrgUnit
				.getValue();
		boolean include = this.chkInclude.isSelected();

		IRowSet rs = HolidayReviewFactory.getRemoteInstance()
				.getHolidayReviewCollection(approveState, beginTime, endTime,
						adminOrg, include);

		String itemId = null;
		while (rs.next()) {
			itemId = rs.getString("VACATIONITEM_ID");
			IRow row = this.tblMain.addRow();

			row.getCell("id").setValue(rs.getString("ID"));
			row.getCell("logId").setValue(rs.getString("LOGID"));
			row.getCell("approveState").setValue(
					ApproveStateEnum.getEnum(rs.getInt("APPROVESTATE")));
			row.getCell("person").setValue(rs.getString("PERSON"));
			row.getCell("personNumber").setValue(rs.getString("PERSONNUMBER"));
			row.getCell("personName").setValue(rs.getString("PERSONNAME"));
			row.getCell("costCenter").setValue(rs.getString("COSTCENTER"));
			row.getCell("attendanceCode").setValue(
					rs.getString("ATTENDANCECODE"));
			row.getCell("adminOrgUnit").setValue(rs.getString("ADMINORGUNIT"));
			row.getCell("vacationItem").setValue(rs.getString("VACATIONITEM"));
			row.getCell("vacationItem").setUserObject(
					rs.getString("VACATIONITEM_ID"));

			row.getCell("unit").setValue(
					UnitTypeEnum.getEnum(rs.getInt("UNITTYPE")));

			if (itemId.equals(Constants.ATTEND_REST_HALF)
					|| itemId.equals(Constants.ATTEND_SARALY_HALF)
					|| itemId.equals(Constants.ATTEND_HOLIDAY)) {
				row.getCell("resultBeginTime").getStyleAttributes()
						.setNumberFormat("yyyy-MM-dd hh:mm:ss");
				row.getCell("resultEndTime").getStyleAttributes()
						.setNumberFormat("yyyy-MM-dd hh:mm:ss");
			} else {
				row.getCell("resultBeginTime").getStyleAttributes()
						.setNumberFormat("yyyy-MM-dd");
				row.getCell("resultEndTime").getStyleAttributes()
						.setNumberFormat("yyyy-MM-dd");
			}

			row.getCell("resultBeginTime").setValue(rs.getDate("BEGINTIME"));
			row.getCell("resultEndTime").setValue(rs.getDate("ENDTIME"));
			row.getCell("resultLength").setValue(rs.getString("HOLIDAYLONG"));

			if (rs.getInt("APPROVESTATE") != ApproveStateEnum.APPROVE_VALUE) {
				row.getCell("resultLength").getStyleAttributes().setLocked(
						false);
				row.getCell("resultLength").getStyleAttributes().setBackground(
						Color.WHITE);
			}

			row.getCell("remark").setValue(rs.getString("REMARK"));

			row.getCell("date").getStyleAttributes().setNumberFormat(
					"yyyy-MM-dd");
			row.getCell("date").setValue(rs.getDate("FDATE"));

			if (ApproveStateEnum.getEnum(rs.getInt("APPROVESTATE")).equals(
					ApproveStateEnum.APPROVE)) {
				row.getStyleAttributes().setBackground(
						Constants.CANNOT_INPUT_COLOR);
				row.getStyleAttributes().setLocked(true);
			}

			// row.setUserObject();
		}
	}

	@Override
	public void actionAudit_actionPerformed(ActionEvent e) throws Exception {
		auditHolidayReviewCollection(ApproveStateEnum.APPROVE);
		btnQuery_actionPerformed(e);
	}

	@Override
	public void actionUnAudit_actionPerformed(ActionEvent e) throws Exception {
		auditHolidayReviewCollection(ApproveStateEnum.NOT_APPROVE);
		btnQuery_actionPerformed(e);
	}

	private void auditHolidayReviewCollection(ApproveStateEnum approve)
			throws Exception {
		int[] rows = KDTableUtilEx.getSelectedRows(this.tblMain);

		HolidayReviewInfo info = null;
		CoreBaseCollection collection = new CoreBaseCollection();

		for (int i = 0; i < rows.length; i++) {

			info = new HolidayReviewInfo();
			IRow row = this.tblMain.getRow(rows[i]);

			if (approve.equals((ApproveStateEnum) row.getCell("approveState")
					.getValue())) {
				continue;
			}

			Object cv = row.getCell("id").getValue();
			if (cv != null) {
				info.setId(BOSUuid.read(cv.toString()));
			}

			info.setApproveState(approve);
			cv = row.getCell("logId").getValue();
			if (cv != null) {
				HolidayLogInfo logInfo = new HolidayLogInfo();
				logInfo.setId(BOSUuid.read(cv.toString()));

				String vacationItemId = (String) row.getCell("vacationItem")
						.getUserObject();

				TimeAttendanceInfo holidayTypeInfo = new TimeAttendanceInfo();
				holidayTypeInfo.setId(BOSUuid.read(vacationItemId));
				logInfo.setHolidayType(holidayTypeInfo);

				String personId = (String) row.getCell("person").getValue();
				PersonInfo person = new PersonInfo();
				person.setId(BOSUuid.read(personId));
				person.setNumber((String) row.getCell("personNumber")
						.getValue());
				logInfo.setPerson(person);

				info.setHolidayLog(logInfo);

				if (row.getCell("resultLength").getValue() != null) {
					Double d = Double.parseDouble(row.getCell("resultLength")
							.getValue().toString());
					info.setVacationLong(d.floatValue());
				}
			}
			cv = row.getCell("remark").getValue();
			info.setRemark((cv == null ? null : cv.toString()));

			cv = row.getCell("date").getValue();
			info.setDate(DateUtil.parse(cv.toString(), "yyyy-MM-dd"));
			collection.add(info);
		}
		HolidayReviewFactory.getRemoteInstance().saveBatchData(collection);

	}

	/**
	 * output actionPageSetup_actionPerformed
	 */
	public void actionPageSetup_actionPerformed(ActionEvent e) throws Exception {
		super.actionPageSetup_actionPerformed(e);
	}

	/**
	 * output actionExitCurrent_actionPerformed
	 */
	public void actionExitCurrent_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExitCurrent_actionPerformed(e);
	}

	/**
	 * output actionHelp_actionPerformed
	 */
	public void actionHelp_actionPerformed(ActionEvent e) throws Exception {
		super.actionHelp_actionPerformed(e);
	}

	/**
	 * output actionAbout_actionPerformed
	 */
	public void actionAbout_actionPerformed(ActionEvent e) throws Exception {
		super.actionAbout_actionPerformed(e);
	}

	/**
	 * output actionOnLoad_actionPerformed
	 */
	public void actionOnLoad_actionPerformed(ActionEvent e) throws Exception {
		super.actionOnLoad_actionPerformed(e);
	}

	/**
	 * output actionSendMessage_actionPerformed
	 */
	public void actionSendMessage_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionSendMessage_actionPerformed(e);
	}

	/**
	 * output actionCalculator_actionPerformed
	 */
	public void actionCalculator_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionCalculator_actionPerformed(e);
	}

	/**
	 * output actionExport_actionPerformed
	 */
	public void actionExport_actionPerformed(ActionEvent e) throws Exception {
		super.actionExport_actionPerformed(e);
	}

	/**
	 * output actionExportSelected_actionPerformed
	 */
	public void actionExportSelected_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelected_actionPerformed(e);
	}

	/**
	 * output actionRegProduct_actionPerformed
	 */
	public void actionRegProduct_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRegProduct_actionPerformed(e);
	}

	/**
	 * output actionPersonalSite_actionPerformed
	 */
	public void actionPersonalSite_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPersonalSite_actionPerformed(e);
	}

	/**
	 * output actionProcductVal_actionPerformed
	 */
	public void actionProcductVal_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionProcductVal_actionPerformed(e);
	}

	/**
	 * output actionExportSave_actionPerformed
	 */
	public void actionExportSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSave_actionPerformed(e);
	}

	/**
	 * output actionExportSelectedSave_actionPerformed
	 */
	public void actionExportSelectedSave_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExportSelectedSave_actionPerformed(e);
	}

	/**
	 * output actionKnowStore_actionPerformed
	 */
	public void actionKnowStore_actionPerformed(ActionEvent e) throws Exception {
		super.actionKnowStore_actionPerformed(e);
	}

	/**
	 * output actionAnswer_actionPerformed
	 */
	public void actionAnswer_actionPerformed(ActionEvent e) throws Exception {
		super.actionAnswer_actionPerformed(e);
	}

	/**
	 * output actionRemoteAssist_actionPerformed
	 */
	public void actionRemoteAssist_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionRemoteAssist_actionPerformed(e);
	}

	/**
	 * output actionPopupCopy_actionPerformed
	 */
	public void actionPopupCopy_actionPerformed(ActionEvent e) throws Exception {
		super.actionPopupCopy_actionPerformed(e);
	}

	/**
	 * output actionHTMLForMail_actionPerformed
	 */
	public void actionHTMLForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForMail_actionPerformed(e);
	}

	/**
	 * output actionExcelForMail_actionPerformed
	 */
	public void actionExcelForMail_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForMail_actionPerformed(e);
	}

	/**
	 * output actionHTMLForRpt_actionPerformed
	 */
	public void actionHTMLForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionHTMLForRpt_actionPerformed(e);
	}

	/**
	 * output actionExcelForRpt_actionPerformed
	 */
	public void actionExcelForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionExcelForRpt_actionPerformed(e);
	}

	/**
	 * output actionLinkForRpt_actionPerformed
	 */
	public void actionLinkForRpt_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionLinkForRpt_actionPerformed(e);
	}

	/**
	 * output actionPopupPaste_actionPerformed
	 */
	public void actionPopupPaste_actionPerformed(ActionEvent e)
			throws Exception {
		super.actionPopupPaste_actionPerformed(e);
	}

	@Override
	public void onLoad() throws Exception {
		super.onLoad();

		this.btnExceptionConvert.setVisible(false);

		this.tblMain.checkParsed();
		KDTMergeManager hm = this.tblMain.getHeadMergeManager();
		hm.setMergeMode(KDTMergeManager.FREE_MERGE);
//		this.tblMain.getSelectManager().setSelectMode(
//				KDTSelectManager.ROW_SELECT
//						| KDTSelectManager.MULTIPLE_ROW_SELECT);

		this.tblMain.getColumn("id").getStyleAttributes().setHided(true);
		this.tblMain.checkParsed();
		this.prmtAdminOrgUnit.setValue(PersonUtil.getCurrentDept());
		this.initSelector();
	}

	private void initSelector() {
		TimeAttendanceUtils.initF7Selector(this, this.prmtAdminOrgUnit);
	}

	@Override
	protected void btnSelectedCopy_actionPerformed(ActionEvent arg0)
			throws Exception {

		KDTableUtilEx.selectedColumnCopy(this.tblMain);
	}

}